package com.ethan.cj.ssm.service.impl;

import com.ethan.cj.ssm.dao.MechanismDao;
import com.ethan.cj.ssm.entity.Mechanism;
import com.ethan.cj.ssm.service.MechanismService;
import com.ethan.cj.ssm.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;


@Service
public class MechanismServiceImpl implements MechanismService {

    @Autowired
    private MechanismDao mechanismDao;

    @Override
    public List<Mechanism> listMechanism() {
        return mechanismDao.listMechanism();
    }

    /**
     * 分页查询
     *
     * @param page
     * @param limit
     * @param map
     * @return
     */
    @Override
    public Map<Object, Object> getMechanismList(int page, int limit, Map<String, Object> map) {
        Map<Object, Object> result = new HashMap<>();
        int currentIndex = (page - 1) * limit;
        map.put("currentIndex", currentIndex);
        map.put("pageSize", limit);
        List<Mechanism> list = mechanismDao.getMechanismList(map);
        int count = mechanismDao.getTotal(map);
        if (list != null && list.size() > 0) {
            result.put("code", 0);
            result.put("msg", "");
            result.put("data", list);
            result.put("count", count);
        } else {
            result.put("code", 1);
            result.put("msg", "无数据");
        }
        return result;
    }

    /**
     * 增加一条数据
     *
     * @param mechanismJson
     * @return
     */
    @Override
    public Map<Object, Object> addMechanism(String mechanismJson) {
        Mechanism mechanism = JsonUtil.parseObject(mechanismJson, Mechanism.class);
        mechanism.setMnumber(ss());
        Map<Object, Object> result = new HashMap<>();
        if (mechanism != null) {
            int rst = mechanismDao.addMechanism(mechanism);
            if (rst > 0) {
                result.put("code", 1);
                result.put("msg", "增加成功！");
            } else {
                result.put("code", 0);
                result.put("msg", "增加失败");
            }
        } else {
            result.put("code", 0);
            result.put("msg", "机构信息JSON解析失败！");
        }
        return result;
    }

    /**
     * 删除一条信息
     *
     * @param mid 机构ID
     * @return
     */
    @Override
    public Map<Object, Object> deleteMechanism(int mid) {
        int code = mechanismDao.deleteMechanism(mid);
        Map<Object, Object> jsonObject = new HashMap<>();
        if (code > 0) {
            jsonObject.put("code", 1);
            jsonObject.put("msg", "成功删除该条信息！");
        } else {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "成功删除该条信息！");
        }
        return jsonObject;
    }

    /**
     * 根据id来查找机构详细信息
     * @param mid
     * @return
     */
    @Override
    public Mechanism selectMechanismMid(int mid) {
        return mechanismDao.selectMechanismMid(mid);
    }

    /**
     * 修改一条信息
     * @param mechanismJson
     * @return
     */
    @Override
    public Map<Object, Object> updateMechanism(String mechanismJson) {
        Mechanism Mechanism = JsonUtil.parseObject(mechanismJson, Mechanism.class);
        int rst = mechanismDao.updateMechanism(Mechanism);
        Map<Object, Object> result = new HashMap<>();
        if (rst > 0) {
            result.put("code", 1);
            result.put("msg", "成功修改信息！");
        } else {
            result.put("code", 0);
            result.put("msg", "修改信息失败！");
        }
        return result;
    }

    /**
     * 生成FW+6位数字
     * @return FW+6位数字
     */
    @Override
    public String ss() {
        String fw = "FW";
        Random r = new Random();
        int six =r.nextInt(900000)+100000;
        return fw+""+six;
    }
}